from django.contrib.auth.mixins import LoginRequiredMixin
from django import http
from django.views import View

from .errmsg_info import error_no, error_msg


# 以下两种--类和装饰器都可以判断用户是否登录
class LoginRequiredJsonMixin(LoginRequiredMixin, View):
    def handle_no_permission(self):
        return http.JsonResponse({'errno': error_no['SESSIONERR'], 'errmsg': error_msg['SESSIONERR']})


def login_decorator(view):
    def wrapper(request, *args, **kwargs):
        if not request.user.is_authenticated:
            return http.JsonResponse({'errno': error_no['SESSIONERR'], 'errmsg': error_msg['SESSIONERR']})
        return view(request, *args, **kwargs)

    return wrapper


